package org.bot.plugin.web.event;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.bot.api.event.DataStorage;
import org.bot.api.event.EventName;
import org.bot.api.event.Events;
import org.bot.api.flow.FlowNode;
import org.bot.plugin.web.util.SelectorType;
import org.bot.plugin.web.util.WebApi;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 沧月
 * @create 0014 2023/9/14 09:11
 * @description <获取列表或者表格之类的多行数据>
 */

@Component
@EventName("爬取列表")
public class WebGetList implements Events {

    @Autowired
    WebApi webApi;

    @Override
    public Object exec(FlowNode note, DataStorage data) {
        JSONArray argsArray = JSONArray.parseArray(note.getArg());
        List<ListXpath> index = new ArrayList<>();
        for (Object o : argsArray) {
            index.add(new ListXpath((JSONObject) o));
        }

        JSONArray array = new JSONArray();
        if (index.isEmpty()) {
            return array;
        }
        WebDriver webDriver = webApi.getWebDriver(note);
        while (true) {
            try {
                JSONObject dt = new JSONObject();
                for (ListXpath listXpath : index) {
                    String text;
                    switch (listXpath.getType()) {
                        case "attr": //属性值
                            text = webApi.getAttribute(webDriver, SelectorType.XPATH, listXpath.getXpath(), listXpath.attrName);
                            break;
                        case "text": //文本
                        default:
                            text = webApi.getText(webDriver, SelectorType.XPATH, listXpath.getXpath());
                    }
                    dt.fluentPut(listXpath.name, text);
                }
                array.add(dt);
            } catch (Exception e) {
                return array;
            }
        }
    }

    class ListXpath {
        private List<Integer> indexList = new ArrayList<>();
        String name;
        private String xpath;
        private String type;
        private String attrName;
        private Pattern pattern = Pattern.compile("\\*\\d+");

        public ListXpath(JSONObject config) {
            String xpath = config.getString("xpath");
            this.name = config.getString("name");
            this.type = config.getString("type");
            this.attrName = config.getString("attrName");
            this.extractAndReplaceStars(xpath);
        }

        public String getType() {
            return this.type;
        }

        public String getAttrName() {
            return attrName;
        }

        public String getXpath() {
            String tmpXpath = xpath;
            for (int i = 0; i < indexList.size(); i++) {
                tmpXpath = tmpXpath.replace("*" + i, String.valueOf(indexList.get(i)));
                indexList.set(i, indexList.get(i) + 1);
            }
            return tmpXpath;
        }


        private void extractAndReplaceStars(String input) {
            Matcher matcher = pattern.matcher(input);
            int index = 0;
            StringBuffer replacedString = new StringBuffer();
            while (matcher.find()) {
                String star = matcher.group();
                indexList.add(Integer.valueOf(star.replace("*", "")));

                // 替换为从0开始的递增数字
                String replacement = "*" + index;
                matcher.appendReplacement(replacedString, replacement);
                index++;
            }
            matcher.appendTail(replacedString);
            this.xpath = replacedString.toString();
        }

    }
}
